package com.ty.tour.controller;


import com.ty.tour.common.utils.ReturnData;
import com.ty.tour.pojo.UserInfo;
import com.ty.tour.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @MethodName: 用户Controller控制器
 * @Author: xiexiaoping
 * @Date: 2020/3/19 14:15
 **/
@Api(value = "用户记录controller", tags = "用户记录查看编辑")
@RestController
@RequestMapping("/user-info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /***
     * @MethodName: 添加用户 /  修改用户信息
     * @Description:
     * @Param: [user]
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/19 14:29
     **/
    @PostMapping("/save")
    @ApiOperation(value = "用户注册", notes = "用户实体")
    public ReturnData save(@RequestBody UserInfo user) {
        final ReturnData returnData = new ReturnData();
        boolean enabled = false;
        if (user == null) {
            return returnData.fail();
        }
        if (user.getId() != null) {
            UserInfo oldUser = userInfoService.getById(user.getId());
            if (oldUser == null) {
                return returnData.fail();
            }
            oldUser.setName(user.getName());
            oldUser.setPassword(user.getPassword());
            oldUser.setUname(user.getUname());
            oldUser.setSex(user.getSex());
            oldUser.setPhone(user.getPhone());
            enabled = userInfoService.updateById(oldUser);
            return enabled == false ? returnData.fail() : returnData.statusTool(1);
        } else {
//            user.setId(0);
            enabled = userInfoService.saveUser(user);
            return enabled == true ? returnData.statusTool(1) : returnData.fail();
        }
    }

    /***
     * @MethodName: login
     * @Description: 用户登录
     * @Param: [name, password]
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/19 14:42
     **/
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户实体")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名", name = "name", dataType = "String", paramType = "query", required = true)
            , @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", paramType = "query", required = true)
    })
    public ReturnData login(HttpSession session, String name, String password) {
        final ReturnData returnData = new ReturnData();
        try {
            if (StringUtils.isAnyBlank(name, password)) {
                return returnData.fail();
            }
            UserInfo user = userInfoService.userLogin(name, password);
            if (user == null) {
                return returnData.fail();
            }
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(60 * 60 * 24);//设置session过期时间
            return returnData.success(user, 1);
        } catch (Exception e) {
            e.printStackTrace();
            return returnData.fail();
        }
    }

    /***
     * @MethodName: queryInfo
     * @Description:用户查看个人信息
     * @Param: []
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/19 16:31
     **/
    @PostMapping("query-info")
    @ApiOperation(value = "用户查看个人信息", notes = "用户实体")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "id", dataType = "Interger", paramType = "query", required = true)
    })
    public ReturnData queryInfo(HttpSession session, Integer id) {
        final ReturnData returnData = new ReturnData();
        try {
            Object obj = session.getAttribute("user");
            if (obj == null) {
                return returnData.fail("您还没有登录，请先登录");
            }
            if (id == null) {
                return returnData.fail("查询失败");
            }
            UserInfo user = userInfoService.getById(id);
            if (user == null) {
                return returnData.fail("用户不存在");
            }
            return returnData.success(user, 1);
        } catch (Exception e) {
            e.printStackTrace();
            return returnData.fail();
        }
    }
}
